﻿using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using Common;
using Common.Components;
using MobileSales.Interface;
using MobileSales.Presenter;

namespace MobileSales
{

    public partial class SettingsForm : Form, ISettingsView
    {
        public SettingsForm()
        {
            InitializeComponent();
            new SettingsPresenter(this);
            isSaveCheckBox_CheckStateChanged(null, null);
        }
       
        private void TextBox_GotFocus(object sender, EventArgs e)
        {
            inputPanel.Enabled = true;
        }

        private void TextBox_LostFocus(object sender, EventArgs e)
        {
            inputPanel.Enabled = false;
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            SaveChanges(null, null);
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        #region ISettingsForm Members

        public string DbPath
        {
            get
            {
                return pathDatabaseTextBox.Text;
            }
            set
            {
                pathDatabaseTextBox.Text = value;
            }
        }

        public string DbPassword
        {
            get
            {
                return passwordDatabasetextBox.Text;
            }
            set
            {
                passwordDatabasetextBox.Text = value;
            }
        }
        public string ImportPath
        {
            get
            {
                return pathImportTextBox.Text;
            }
            set
            {
                pathImportTextBox.Text = value;
            }
        }
        public string ExportPath
        {
            get
            {
                return pathExportTextBox.Text;
            }
            set
            {
                pathExportTextBox.Text = value;
            }
        }

        public bool IsSaveFile
        {
            get
            {
                return isSaveCheckBox.Checked;
            }
            set
            {
                isSaveCheckBox.Checked = value;
            }
        }

        public string ArcImportPath
        {
            set
            {
                pathArcImportTextBox.Text = value;
            }
            get
            {
                return pathArcImportTextBox.Text;
            }
        }

        public string ArcExportPath
        {
            set
            {
                pathArcExportTextBox.Text = value;
            }
            get
            {
                return pathArcExportTextBox.Text;
            }
        }

        public bool VisibleNullGoods 
        {
            get
            {
                return visibleNullGoodsCheckBox.Checked;
            }
            set
            {
                visibleNullGoodsCheckBox.Checked = value;
            }
        }
        public bool CheckRemainingGoods 
        {
            get 
            {
                return checkRemainingGoodsCheckBox.Checked;
            }
            set             
            {
                checkRemainingGoodsCheckBox.Checked = value;
            }
        }

        public event EventHandler<EventArgs> SaveChanges;

        public event EventHandler<EventArgs> CancelChanges;

        #endregion

        private void cancelButton_Click(object sender, EventArgs e)
        {
            CancelChanges(null, null);
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;

        }

        private string selectPath()
        {
            OpenFileDialogEx openFileDialog = new OpenFileDialogEx(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase), true);
            openFileDialog.Text = "Вкажіть шлях";
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                return openFileDialog.SelectedFullName;
            }
            return null;
        }

        private void pathDatabaseButton_Click(object sender, EventArgs e)
        {
            OpenFileDialogEx openFileDialog = new OpenFileDialogEx(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase), "*.sdf", false);
            openFileDialog.Text = "Вкажіть шлях до бази";
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                pathDatabaseTextBox.Text = openFileDialog.SelectedFullName;
            }

        }

        private void importPathButton_Click(object sender, EventArgs e)
        {
            string path = selectPath();
            pathImportTextBox.Text = path != null?path:string.Empty;
        }

        private void exportPathButton_Click(object sender, EventArgs e)
        {
            string path = selectPath();
            pathExportTextBox.Text = path != null ? path : string.Empty;
        }

        private void isSaveCheckBox_CheckStateChanged(object sender, EventArgs e)
        {
            saveParamsPanel.Visible = isSaveCheckBox.Checked;
        }

        private void pathArcImportButton_Click(object sender, EventArgs e)
        {
            string path = selectPath();
            pathArcImportTextBox.Text = path != null ? path : string.Empty;
        }

        private void pathArcExportButton_Click(object sender, EventArgs e)
        {
            string path = selectPath();
            pathArcExportTextBox.Text = path != null ? path : string.Empty;
        }


    }
}